require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#commentInfo",
    data:{
        number:"",
        name:"",//回复name
        type:"1",
        loadFlag:false,
        id:"", //视频id
        uid:"",  //用户id
        commentId:"", //评论id
        ruserId:"",
        pageNumber:1,
        commentText:"",
        commentList:{},
        // commentList1:[],
        commentReplyList:{},
        commentReplyList1:[],
        commentReplyList2:{},
        praiseCount:"",//点赞人数
        praiseState:""//是否点赞
    },
    mounted:function(){
        this.searchObject = this.getQueryStringArgs();
        this.id=this.searchObject&&this.searchObject.id;
        this.uid=this.searchObject&&this.searchObject.loginUid;
        this.commentId=this.searchObject&&this.searchObject.commentId;
        this.number=this.searchObject&&this.searchObject.type;
        if(this.number&&this.number==2){
            this.getInfoPostCommentByCommentId();
            this.getInfoPostCommentReplyList();
        }else{
            this.getAdvCommentsByCommentId();
            this.getInfoMediaAdvCommentReplyList();
        }

    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"/imgs/see/index/index-bgimg1.png";
            return {
                "background-image":'url("'+url+'"),url("/imgs/see/index/index-bgimg1.png")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //获取自媒体-评论详情
        getAdvCommentsByCommentId:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                commentId:that.commentId
                // pageNumber:that.pageNumber
            };
            //console.log(data)
            this.ajax({url:"/selfMedia/getAdvCommentsByCommentId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==00&&b.json.data){
                    that.commentList=b.json.data;
                    that.praiseCount=that.commentList.praiseCount;
                    that.praiseState=that.commentList.praiseState;
                }else{
                    vm.$refs.prompt.toast("获取评论列表！");
                }
            });
        },
        //评论点赞
        praiseMediaAdvComment:function(){
            var that=this,url,data;
            data={
                loginUid:that.uid,
                commentId :that.commentId
            };
            if(that.praiseState==0){
                data.state=1;
            }else{
                data.state=0;
            }
            if(that.number==2){
                url="/friend/praisePostComment";
            }else{
                url="/selfMedia/praiseMediaAdvComment";
            }
            console.log(data)
            this.ajax({url:url,data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    if(that.number==2){
                        that.getInfoPostCommentByCommentId();
                    }else{
                        that.getAdvCommentsByCommentId();
                    }
                }else{
                    vm.$refs.prompt.toast("点赞失败！");
                }
            });
        },
        //获取自媒体回复评论列表
        getInfoMediaAdvCommentReplyList:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                id:that.id,
                pageNumber:that.pageNumber,
                commentId:that.commentId
            };
            //console.log(data)
            this.ajax({url:"/selfMedia/getInfoMediaAdvCommentReplyList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==00&&b.json.data){
                    that.commentReplyList=b.json.data;
                    that.commentReplyList1=that.commentReplyList.list;
                    if(that.commentReplyList.pageNumber>1){
                        if(that.commentReplyList.list.length>0) {
                            that.commentReplyList2 = that.commentReplyList2.concat(that.commentReplyList1);
                        }
                    }else{
                        that.commentReplyList2= that.commentReplyList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.commentReplyList2)
                }else{
                    that.commentReplyList={};
                    that.commentReplyList1=[];
                    that.commentReplyList2=[];
                    vm.$refs.prompt.toast("获取评论列表！");
                }
            });
        },
        //回复
        getReply:function(userId,name){
            if(userId&&name){
                this.ruserId=userId;
                this.name=name;
                if(this.ruserId!=userInfo.chatId){
                    this.type=2;
                }else{
                    this.ruserId="";
                    this.name="";
                    this.type=1;
                }
            }else{
                this.type=1;
                this.ruserId="";
                this.name="";
            }
            console.log(this.ruserId+"-------"+this.name+"-------"+this.type)
        },
        //自媒体-发表评论
        comment:function(){
            var that=this;
            if(!that.commentText){
                vm.$refs.prompt.toast("请输入评论内容！");
                return false;
            }
            var data={
                loginUid:that.uid,
                content:that.commentText,
                id:that.id,
                commentId:that.commentId
            };
            if(that.ruserId!=that.uid){
                data.ruserId=that.ruserId;
            }
            console.log(data);
            this.ajax({url:"/selfMedia/comment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.commentText="";
                    that.getAdvCommentsByCommentId();
                    that.getInfoMediaAdvCommentReplyList();
                    // console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("评论失败！");
                }
            });
        },
        //帖子-发表评论
        postComment:function(){
            var that=this;
            if(!that.commentText){
                vm.$refs.prompt.toast("请输入评论内容！");
                return false;
            }
            var data={
                loginUid:that.uid,
                content:that.commentText,
                postId :that.id,
                commentId:that.commentId
            };
            if(that.ruserId!=that.uid){
                data.ruserId=that.ruserId;
            }
            console.log(data);
            this.ajax({url:"/friend/postComment",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.commentText="";
                    that.getInfoPostCommentByCommentId();
                    that.getInfoPostCommentReplyList();
                    // console.log(b.json.data)
                }else{
                    vm.$refs.prompt.toast("评论失败！");
                }
            });
        },
        //跳转页面
        getNext:function(recruitId){
            location.href="/see/jobDetails?recruitId="+recruitId;
        },
        //获取帖子-评论详情
        getInfoPostCommentByCommentId:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                commentId:that.commentId
                // pageNumber:that.pageNumber
            };
            //console.log(data)
            this.ajax({url:"/friend/getInfoPostCommentByCommentId",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    that.commentList=b.json.data;
                    that.praiseCount=that.commentList.praiseCount;
                    that.praiseState=that.commentList.praiseState;
                }else{
                    vm.$refs.prompt.toast("获取评论详情失败！");
                }
            });
        },
        //获取帖子回复评论列表
        getInfoPostCommentReplyList:function(){
            var that=this;
            var data={
                loginUid:that.uid,
                pageNumber:that.pageNumber,
                commentId:that.commentId
            };
            //console.log(data)
            this.ajax({url:"/friend/getInfoPostCommentReplyList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==00&&b.json.data){
                    that.commentReplyList=b.json.data;
                    that.commentReplyList1=that.commentReplyList.list;
                    if(that.commentReplyList.pageNumber>1){
                        if(that.commentReplyList.list.length>0) {
                            that.commentReplyList2 = that.commentReplyList2.concat(that.commentReplyList1);
                        }
                    }else{
                        that.commentReplyList2= that.commentReplyList1;
                    }
                    vm.loadFlag=false;
                    console.log(that.commentReplyList2)
                }else{
                    that.commentReplyList={};
                    that.commentReplyList1=[];
                    that.commentReplyList2=[];
                    vm.$refs.prompt.toast("获取评论列表！");
                }
            });
        }
    }
    ,watch:{
        commentList2:function(){
            this.$nextTick(function(){
                console.log($(".comment-cent").height());
                //getScroll();
            });
        }
    }

});
//文档高度
function getDocumentTop() {
    var scrollTop = 0, bodyScrollTop = 0, documentScrollTop = 0;
    if (document.body) {
        bodyScrollTop = document.body.scrollTop;
    }
    if (document.documentElement) {
        documentScrollTop = document.documentElement.scrollTop;
    }
    scrollTop = (bodyScrollTop - documentScrollTop > 0) ? bodyScrollTop : documentScrollTop;    return scrollTop;
}
//可视窗口高度
function getWindowHeight() {
    var windowHeight = 0;
    if (document.compatMode == "CSS1Compat") {
        windowHeight = document.documentElement.clientHeight;
    } else {
        windowHeight = document.body.clientHeight;
    }
    return windowHeight;
}
//滚动条滚动高度
function getScrollHeight() {
    var scrollHeight = 0, bodyScrollHeight = 0, documentScrollHeight = 0;
    if (document.body) {
        bodyScrollHeight = document.body.scrollHeight;
    }
    if (document.documentElement) {
        documentScrollHeight = document.documentElement.scrollHeight;
    }
    scrollHeight = (bodyScrollHeight - documentScrollHeight > 0) ? bodyScrollHeight : documentScrollHeight;
    return scrollHeight;

}
window.onscroll=function(){
    var loadFlag = vm.commentReplyList.lastPage;
    //监听事件内容
    if(getScrollHeight()==getWindowHeight() + getDocumentTop()){
        //当滚动条到底时,这里是触发内容
        if(!loadFlag){
            vm.loadFlag=true;
            vm.pageNumber++;
            vm.getInfoMediaAdvCommentReplyList();
        }else{
            vm.$refs.prompt.toast("没有更多了！")
        }
    }
};